﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class CommandButtonForm : ItemFormTemplate
    {
        public CommandButtonForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form_Load);
            this.Shown += new System.EventHandler(this.Form_Shown);
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            System.Threading.Thread.Sleep(300);
            view.ViewData = DemoHelper.GetDescription(AppDomain.CurrentDomain.BaseDirectory + "例子\\控件\\CommandButtonForm\\CommandButtonForm.rtf");
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {
            this.introduceBrowser.Rtf = viewObject.ViewData.ToString();
        }

        public override void ShowView()
        {
            base.ShowView();
        }

        public override void HideView()
        {
            base.HideView();
        }

        public override void CloseView()
        {
            base.CloseView();
        }

        #endregion

        #region 属性面板

        private void Form_Load(object sender, EventArgs e)
        {
            this.controlContainer.SizeChanged += this.ControlContainer_SizeChanged;

            this.demo_control.RoundEnabled = true;
            this.demo_control.AutoWidth = false;
            this.demo_control.AutoWidthMin = 10;
            this.demo_control.FixedWidth = 70;
            this.demo_control.TextAlignment = CommandButtonTextAlignment.Center;
            this.demo_control.TextMargin = new Padding(3, 3, 3, 3);
            this.demo_control.Selected += this.Demo_control_SelectedItemChanged;

            this.demo_control_status.Text = "当前选中为：" + (this.demo_control.SelectedCommand == null ? "" : this.demo_control.SelectedCommand.Text);

            this.roundEnabled_toggleSwitch.IsOn = this.demo_control.RoundEnabled;
            this.roundEnabled_toggleSwitch.Toggled += this.RoundEnabled_toggleSwitch_Toggled;

            this.deSelectEnabled_toggleSwitch.IsOn = this.demo_control.DeSelectEnabled;
            this.deSelectEnabled_toggleSwitch.Toggled += this.DeSelectEnabled_toggleSwitch_Toggled;

            this.itemAutoWidth_toggleSwitch.IsOn = this.demo_control.AutoWidth;
            this.itemAutoWidth_toggleSwitch.Toggled += this.ItemAutoWidth_toggleSwitch_Toggled;

            this.itemAutoWidthMin_label.Text = this.demo_control.AutoWidthMin.ToString();
            this.itemAutoWidthMin_trackBarPlus.Value = this.demo_control.AutoWidthMin;
            this.itemAutoWidthMin_trackBarPlus.ValueChanged += this.ItemAutoWidthMin_trackBarPlus_ValueChanged;

            this.itemFixedWidth_label.Text = this.demo_control.FixedWidth.ToString();
            this.itemFixedWidth_trackBarPlus.Value = this.demo_control.FixedWidth;
            this.itemFixedWidth_trackBarPlus.ValueChanged += this.ItemFixedWidth_trackBarPlus_ValueChanged;

            this.itemTextAlignment_dropDownListPlus.Multiple = false;
            this.itemTextAlignment_dropDownListPlus.BindEnumList<CommandButtonTextAlignment>(this.demo_control.TextAlignment);
            this.itemTextAlignment_dropDownListPlus.SelectedItemsChanged += this.ItemTextAlignment_dropDownListPlus_SelectedItemsChanged;

            this.textMargin_top_label.Text = this.demo_control.TextMargin.Top.ToString();
            this.textMargin_top_trackBarPlus.Value = this.demo_control.TextMargin.Top;
            this.textMargin_top_trackBarPlus.ValueChanged += this.TextMargin_top_trackBarPlus_ValueChanged;

            this.textMargin_bottom_label.Text = this.demo_control.TextMargin.Bottom.ToString();
            this.textMargin_bottom_trackBarPlus.Value = this.demo_control.TextMargin.Bottom;
            this.textMargin_bottom_trackBarPlus.ValueChanged += this.TextMargin_bottom_trackBarPlus_ValueChanged;

            this.textMargin_left_label.Text = this.demo_control.TextMargin.Left.ToString();
            this.textMargin_left_trackBarPlus.Value = this.demo_control.TextMargin.Left;
            this.textMargin_left_trackBarPlus.ValueChanged += this.TextMargin_left_trackBarPlus_ValueChanged;

            this.textMargin_right_label.Text = this.demo_control.TextMargin.Right.ToString();
            this.textMargin_right_trackBarPlus.Value = this.demo_control.TextMargin.Right;
            this.textMargin_right_trackBarPlus.ValueChanged += this.TextMargin_right_trackBarPlus_ValueChanged;

        }

        private void Form_Shown(object sender, EventArgs e)
        {
            this.splitContainer.SplitterDistance = Math.Max(this.splitContainer.Width / 4, this.splitContainer.Width - MainForm.GlobalSplitWidth);
            this.splitContainer.SplitterMoved += this.SplitContainer_SplitterMoved;

            MainForm.LoadAssistTabControl(this.assistTabControl);
        }

        private void SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer control = (SplitContainer)sender;
            MainForm.GlobalSplitWidth = this.splitContainer.Width - control.SplitterDistance;
        }

        private void ControlContainer_SizeChanged(object sender, EventArgs e)
        {
            Panel control = (Panel)sender;
            this.demo_control.Location = new Point((control.Width - this.demo_control.Width) / 2, (control.Height - this.demo_control.Height) / 2);
            this.demo_control_status.Location = new Point(this.demo_control.Location.X, this.demo_control.Location.Y + this.demo_control.Height * 3);
        }

        private void Demo_control_SelectedItemChanged(object sender, EventArgs e)
        {
            CommandButton control = (CommandButton)sender;
            this.demo_control_status.Text = "当前选中为：" + (control.SelectedCommand == null ? "" : control.SelectedCommand.Text);
        }

        private void RoundEnabled_toggleSwitch_Toggled(object sender, EventArgs e)
        {
            ToggleSwitch control = (ToggleSwitch)sender;
            this.demo_control.RoundEnabled = control.IsOn;
        }

        private void DeSelectEnabled_toggleSwitch_Toggled(object sender, EventArgs e)
        {
            ToggleSwitch control = (ToggleSwitch)sender;
            this.demo_control.DeSelectEnabled = control.IsOn;
        }

        private void ItemAutoWidth_toggleSwitch_Toggled(object sender, EventArgs e)
        {
            ToggleSwitch control = (ToggleSwitch)sender;
            this.demo_control.AutoWidth = control.IsOn;
        }

        private void ItemAutoWidthMin_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            TrackBarPlus control = (TrackBarPlus)sender;
            this.demo_control.AutoWidthMin = (int)control.Value;
            this.itemAutoWidthMin_label.Text = this.demo_control.AutoWidthMin.ToString();
        }

        private void ItemFixedWidth_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            TrackBarPlus control = (TrackBarPlus)sender;
            this.demo_control.FixedWidth = (int)control.Value;
            this.itemFixedWidth_label.Text = this.demo_control.FixedWidth.ToString();
        }

        private void ItemTextAlignment_dropDownListPlus_SelectedItemsChanged(object sender, EventArgs e)
        {
            DropDownListPlus control = (DropDownListPlus)sender;
            this.demo_control.TextAlignment = (CommandButtonTextAlignment)control.SelectedItem.Tag;
        }

        private void TextMargin_top_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            TrackBarPlus control = (TrackBarPlus)sender;
            this.demo_control.TextMargin = new Padding(this.demo_control.TextMargin.Left, (int)control.Value, this.demo_control.TextMargin.Right, this.demo_control.TextMargin.Bottom);
            this.textMargin_top_label.Text = this.demo_control.TextMargin.Top.ToString();
        }

        private void TextMargin_bottom_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            TrackBarPlus control = (TrackBarPlus)sender;
            this.demo_control.TextMargin = new Padding(this.demo_control.TextMargin.Left, this.demo_control.TextMargin.Top, this.demo_control.TextMargin.Right, (int)control.Value);
            this.textMargin_bottom_label.Text = this.demo_control.TextMargin.Bottom.ToString();
        }

        private void TextMargin_left_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            TrackBarPlus control = (TrackBarPlus)sender;
            this.demo_control.TextMargin = new Padding((int)control.Value, this.demo_control.TextMargin.Top, this.demo_control.TextMargin.Right, this.demo_control.TextMargin.Bottom);
            this.textMargin_left_label.Text = this.demo_control.TextMargin.Left.ToString();
        }

        private void TextMargin_right_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            TrackBarPlus control = (TrackBarPlus)sender;
            this.demo_control.TextMargin = new Padding(this.demo_control.TextMargin.Left, this.demo_control.TextMargin.Top, (int)control.Value, this.demo_control.TextMargin.Bottom);
            this.textMargin_right_label.Text = this.demo_control.TextMargin.Right.ToString();
        }

        #endregion

    }
}
